package me.leon.keeplive

import android.accessibilityservice.AccessibilityService.GLOBAL_ACTION_RECENTS
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import me.leon.keeplive.databinding.ActivityWhiteListBinding

class WhiteListActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //        setContentView(R.layout.activity_white_list)
        val binding: ActivityWhiteListBinding =
            ActivityWhiteListBinding.bind(
                layoutInflater.inflate(R.layout.activity_white_list, null)
            )

        StatusBarHelper.setStatusBarColor(this, android.R.color.white)
        StatusBarHelper.setDarkFontStatusBar(this)

        binding.ivBack.setOnClickListener { finish() }
    }

    override fun onResume() {
        super.onResume()
        if (!OSHelper.hasBatteryOptimization()) {
            OSHelper.goBatteryOptimization()
        }
    }

    companion object {
        fun start(ctx: Context) {
            val intent = Intent(ctx, WhiteListActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            ctx.startActivity(intent)
        }
    }

    fun noBatteryOpt(view: View) {
        if (OSHelper.hasBatteryOptimization()) show("已经开启优化") else OSHelper.goBatteryOptimization()
    }

    fun autoStart(view: View) {
        OSHelper.goAutoStar()
    }

    fun limitless(view: View) {
        if (!OSHelper.goLimitless()) {
            show("该设备无法设置,请到其他权限设置")
        }
    }

    fun show(msg: String) = Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    fun goNotificationSetting(view: View) {

        OSHelper.goNotificationSettings(this)
    }

    fun goRecent(view: View) {
        if (!Svc.isAccessibilitySettingsOn(this)) {
            show("请先开启App无障碍功能")
            return
        }
        Svc.sendAction(this, GLOBAL_ACTION_RECENTS)
    }

    fun goAccessibility(view: View) {
        if (!Svc.isAccessibilitySettingsOn(this)) {
            Log.d("svc", "go settings")
            startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
        } else {
            Log.d("svc", "start svc")
            startService(Intent(this, Svc::class.java))
            show("已启动服务")
        }
    }

    fun goPermission(view: View) {
        OSHelper.goAppDetail()
    }
}
